const api = require("../../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    deviceSn:null,
    devInfo:{},
    agreement:{
        checked:true
    },
    useCommon:false,
    usePrePay:false
  },
  btnPay:function(){
    const self = this;
    if(!this.data.agreement.checked){
        my.showModal({
          title: '租借失败',
          content: '请先勾选同意授权书',
          showCancel:false,
        })
        return
    }
    api.post(api.url.cdbPay,{
        payType:2,
        appId:my.getAppIdSync().appId,
        deviceSn:this.data.deviceSn,
        costMode:this.data.devInfo.costMode
    },(res)=>{
        console.log(res)
        if(res.data.code==200){
          const tradeNo = res.data.body.tradeNo;
          my.tradePay ({
            tradeNO: tradeNo,
            success: res => {
              console.error('调用 my.tradePay 成功: ', JSON.stringify(res));
              self.toUnderway(tradeNo);
            },
            fail: error => {
              console.error('调用 my.tradePay 失败: ', JSON.stringify(error));
              my.showToast({
                content: '支付失败',
                icon:'error',
                mask:true
              })
            },
          });
        }else{
          my.showModal({
              title: '支付失败',
              content: res.data.msg,
              showCancel:false,
              complete: () => {
              my.reLaunch({
                  url: '/pages/newIndex/index',
              })
              }
          })
        }
    })
  },

  btnPrePay:function(){
      const self = this;
      if(!this.data.agreement.checked){
          my.showModal({
            title: '租借失败',
            content: '请先勾选同意授权书',
            showCancel:false,
          })
          return
      }
      api.post(api.url.cdbPay,{
          payType:2,
          appId:my.getAppIdSync().appId,
          deviceSn:this.data.deviceSn,
          costMode:this.data.devInfo.costMode
      },(res)=>{
          console.log(res)
          if(res.data.code==200){
              const tradeNo = res.data.body.tradeNo;
              const payInfo = JSON.parse(res.data.body.payInfo);
              my.openBusinessView({
                  businessType: 'wxpayScoreUse',
                  extraData: {
                      mch_id: payInfo.mch_id,
                      package: payInfo.package,
                      timestamp: payInfo.timestamp + "",
                      nonce_str: payInfo.nonce_str,
                      sign_type: 'HMAC-SHA256',
                      sign: payInfo.sign
                  },
                  envVersion: 'release',
                  success(res) {
                      console.log(res)
                      if (res.errCode == 0 && res.extraData.appid) {
                          self.toUnderway(tradeNo);
                      } else {
                          self.setData({
                              usePrePay:false
                          })
                      }
                  },
                  fail(err) {
                      console.log(err);
                      self.setData({
                          usePrePay:false
                      })
                  },
              });
          }else{
              my.showModal({
                  title: '支付失败',
                  content: res.data.msg,
                  showCancel:false,
                  complete: () => {
                      my.reLaunch({
                          url: '/pages/index/index',
                      })
                  }
              })
          }
      })
  },

  toUnderway(tradeNo){
      console.log(tradeNo);
      my.redirectTo({
        url: '../result/result?tradeNo='+tradeNo,
      })
  },

  toIndex(){
      my.reLaunch({
        url: '/pages/index/index',
      })
  },

  //勾选授权
  btnChangeRadio(){
      const agreement = this.data.agreement;
      agreement.checked = !agreement.checked;
      this.setData({agreement});
  },

  btnSettingUse(){
      const self = this;
      my.showModal({
        title: '常用设备',
        content: '设置为常用设备？',
        complete: (res) => {
          if (res.confirm) {
            api.post(api.url.useDeviceAdd,{
                deviceSn:self.data.deviceSn
              },res=>{
                  if(res.data.code==200){
                    my.showToast({content: '设置成功！',icon:'none'})
                    self.setData({useCommon:false})
                  }else{
                    my.showToast({content: res.data.msg,icon:'error'})
                  }
              })
          }
        }
      })
  },
  onLoad(options) {
    if(options.deviceSn){
      this.setData({
          deviceSn:options.deviceSn,
          useCommon:options.use==1?true:false
      })
    }
    api.post(api.url.cdbScan,{
        deviceSn:options.deviceSn
    },res=>{
        if(res.data.code==200){
            this.setData({
                devInfo:res.data.body,
                usePrePay:res.data.body.costMode == 2?true:false
            })
            if(res.data.body.state == 2){
                my.showModal({
                    title: '温馨提示',
                    content: '设备离线',
                    showCancel:false,
                    success(){
                        my.reLaunch({
                          url: '/pages/index/index',
                        })
                    }
                })
            }
        }else{
            my.showModal({
              content: res.data.msg,
              showCancel:false,
              success(){
                  my.reLaunch({
                    url: '/pages/index/index',
                  })
              }
            })
        }
    })
  },
});
